<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * MY_Session extends the built-in CI_Session
 *
 * This class adds automatic generation of the proper table in the database
 * without the developer having to think about it if the 'sess_use_database'
 * value is set to TRUE in the config/config.php file
 *
 * @author Casey McLaughlin
 * @package CIHeadStart
 * @link http://code.google.com/p/ciheadstart
 */
Class MY_Session extends CI_Session
{
	/**
	 * Constructor
	 *
	 * Setup the database table if sess_use_database is set in the config file,
	 * and the table does not already exist.
	 */
	function __construct()
	{
		//Check database table exists,
		//and if not, attempt to create it
		$ci =& get_instance();

		if ($ci->config->item('sess_use_database') == TRUE)
		{
			$table_name = $ci->config->item('sess_table_name');
			$ci->load->database();

			if ($ci->db->dbprefix != '' && substr($table_name, 0, strlen($ci->db->dbprefix)) != $ci->db->dbprefix)
				$table_name = $ci->db->dbprefix . $table_name;

			if ( ! $ci->db->table_exists($table_name))
			{
				$query = "CREATE TABLE IF NOT EXISTS  `$table_name` (session_id varchar(40) DEFAULT '0' NOT NULL, ip_address varchar(16) DEFAULT '0' NOT NULL,
									user_agent varchar(50) NOT NULL, last_activity int(10) unsigned DEFAULT 0 NOT NULL, user_data text NULL, PRIMARY KEY (session_id));";

				if ( ! $ci->db->query($query))
					show_error("Attempted to build the $table_name database table for session storage, but failed!");
			}
		}
		
		parent::CI_Session();
	}
}

/* End of file MY_Session.php */
/* Location: ./app/libraries/MY_Session.php */